package com.aiwhose.aiwhosewechatplublicaiwhose.service.impl;

import cn.hutool.core.util.StrUtil;
import com.aiwhose.aiwhosecore.bean.plublicaiwhose.bean.WXPlublicUserInfoBean;
import com.aiwhose.aiwhosecore.bean.security.User;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.aiwhose.aiwhosewechatplublicaiwhose.mapper.UserMapper;
import com.aiwhose.aiwhosewechatplublicaiwhose.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 用户表 服务层实现。
 *
 * @author 田国刚
 * @since 1.0.1
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    UserMapper userMapper;

    @Override
    public boolean saveUserByPlublic(WXPlublicUserInfoBean wxPlublicUserInfoBean) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(User::getUnionid, wxPlublicUserInfoBean.getUnionid())
                .eq(User::getSystemType, "AIWHOSE-PLUBLIC")
                .eq(User::getWxOpenId,wxPlublicUserInfoBean.getOpenid());
        long count = userMapper.selectCountByQuery(queryWrapper);

        if (count > 0) {
            return false;
        }
        User user = new User();
        user.setUnionid(wxPlublicUserInfoBean.getUnionid());
        user.setWxOpenId(wxPlublicUserInfoBean.getOpenid());
        if (StrUtil.isBlank(wxPlublicUserInfoBean.getOpenid())) {
            return false;
        }
        user.setSystemType("AIWHOSE-PLUBLIC");
        int insert = userMapper.insert(user);

        return insert > 0;
    }


}
